define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'layui', 'template'], function ($, undefined, Backend, Table, Form, Layui, Template) {
    var school_id = 0, class_id = 0,student_id = 0;

    var Controller = {
        index: function () {
            layui.use(['tree', 'util'], function () {
                var tree = layui.tree;
                tree.render({
                    elem: '#test4',
                    data: Config.admin.school,
                    accordion: true,
                    click: function (obj) {
                        var data = obj.data;
                        if (data.type == 'classes') {
                            $('.selected').removeClass('selected');
                            $(obj.elem).find('.layui-tree-main').addClass('selected');

                            school_id = data.school_id;
                            class_id = data.id;

                            // $('input[name=school_id]').val(school_id)
                            // $('input[name=class_id]').val(class_id)

                            Controller.api.studentList();
                        }

                    }
                });
            });

            $(document).delegate('.student', 'click', function () {
                $('#divcontainer .left').find('.selected').removeClass('selected');
                $(this).parent().addClass('selected');
                student_id = $(this).data('id');
                if ($('.semester_num').val()) {
                    Controller.api.scheduleList();
                }
            });
            //学期
            $(document).on('change', '.semester', function () {
                Controller.api.semesterWeekList();
            });
            //学期周次
            $(document).on('change', '.semester_num', function () {
                if (student_id) {
                    Controller.api.scheduleList();
                }
            });

        },
        api: {
            studentList: function () {
                $.ajax({
                    type: 'post',
                    url: '/admin/course/student/index',
                    cache: false,
                    data: {school_id: school_id, class_id: class_id},
                    dataType: 'json',
                    success: function (data) {
                        $("#divcontainer .left").html(Template("studentTpl", data));
                    },
                    error: function () {
                    }
                });

                if (!$('.semester_num').val()) {
                    Controller.api.semesterList();
                }
            },
            semesterList: function () {
                $(".semester_num").html('');
                $.ajax({
                    type: 'post',
                    url: 'semester/select',
                    cache: false,
                    data: {school_id: school_id},
                    dataType: 'json',
                    success: function (ret) {
                        $(".semester").html(Template("semesterTpl", ret));
                    },
                    error: function () {
                    }
                });
            },
            semesterWeekList: function () {
                $.ajax({
                    type: 'post',
                    url: 'semester/selectWeekList',
                    cache: false,
                    data: {id: $('.semester').val()},
                    dataType: 'json',
                    success: function (ret) {
                        $(".semester_num").html(Template("semesterTpl", ret));
                    },
                    error: function () {
                    }
                });
            },
            scheduleList: function () {
                if (student_id && $('.semester_num').val()) {
                    $.ajax({
                        type: 'post',
                        url: '/admin/course/student/detail',
                        cache: false,
                        data: {school_id: school_id, class_id: class_id, student_id: student_id,semester_id:$('.semester').val(),semester_num:$('.semester_num').val()},
                        dataType: 'json',
                        success: function (ret) {
                            $("#divcontainer .right").html(Template("classroomTpl", ret));
                        },
                        error: function () {
                        }
                    });
                }
            }
        }
    };
    return Controller;
});